//服务器接口地址
// const baseURL : string = 'http://192.168.0.46:19999'
const baseURL : string = 'http://192.168.0.14:9999'
//本地调试接口地址
// const baseURL:string='http://xxxxxx'
// 封装公共请求方法

		// 验证登录情况
		function ajaxFilter(data) {
		  let status = true
		  if (data.code == 'A0230') {
		    status = false
		 uni.showToast({
		 	title: '登录状态失效',
		 	icon: 'none'
		 })
		 uni.removeStorageSync('token')
		 uni.reLaunch({
		   url: '/pages/login/index',
		 })
		    // uni.showToast('登录状态失效', () => {
		    //   uni.removeStorageSync('token')
		    //   uni.reLaunch({
		    //     url: '/pages/login/index',
		    //   })
		    // })
		  }
		  return status
		}
function request(url : string, method : "GET" | "POST" | undefined, data : object | any) {
	
	return new Promise(function (resolve, reject) {
		    uni.showLoading({
		      title: '加载中',
		      mask: true,
		    })
		let header : any
		// if (uni.getStorageSync('token') !== undefined && uni.getStorageSync('token') !== "") {
		// 	header = {
		// 		'content-type': 'application/json',
		// 		'Token': uni.getStorageSync('token')
		// 	};
		// } else {
		// 	header = {
		// 		'content-type': 'application/json',
		// 	};
		// }
		const token = uni.getStorageSync('token')
		const token_type = uni.getStorageSync('token_type')
		  if (token) {
		      header={
				  'content-type': 'application/json',
				  'Authorization': `${token_type} ` + `${token}`
				  }
		  } else {
		    header = {
		      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8;',
		      'Authorization': 'Basic emd5cDoxMjMzMjE=',
			// 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
			//   'Authorization': 'Basic bWFsbC1hcHA6MTIzNDU2'
		    }
		  }
		uni.request({
			url: baseURL + url,
			method: method,
			data: data,
			header: header,
			success(res : any) {	
				console.log(res)
				uni.hideLoading()
				   if (ajaxFilter(res.data) && res.statusCode == 200) {
				          resolve(res.data)
				        }else if (res.statusCode != 401){
					//其他异常
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
					reject(res.data);
				
				}else if(res.statusCode == 401){
				   uni.removeStorageSync('token')
				}
		
			},
			fail(err) {
				
				uni.hideLoading()
				//请求失败
				uni.showToast({
					title: '无法连接到服务器',
					icon: 'none'
				})
				reject(err)
			}
		})
	})
}
export { request, baseURL }
